import { GET_CENTREINFO } from "@/api/commission";

const commission = {
  state: {
    info: null
  },
  mutations: {
    setCommissionInfo(state, info) {
      state.info = info
    }
  },
  actions: {
    /**
     * 获取分销商信息
     * @param {Boolean} update  更新分销商信息，不传则有信息情况读已有信息
     */
    getCommissionInfo(context, update) {
      return new Promise((resolve, reject) => {
        function getInfo() {
          GET_CENTREINFO().then(({ data }) => {
            context.commit('setCommissionInfo', data)
            resolve(data)
          }).catch(error => {
            reject(error)
          })
        }
        if (update) {
          getInfo()
        } else {
          if (context.state.info) {
            context.commit('setCommissionInfo', context.state.info)
            resolve(context.state.info)
          } else {
            getInfo()
          }
        }
      })
    }
  }
}

export default commission